package com.lsege.controller.sys;

import com.lsege.controller.BaseController;
import com.lsege.entity.JsonResult;
import com.lsege.entity.sys.AppMenu;
import com.lsege.entity.sys.HelpingInfo;
import com.lsege.service.sys.HomeService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/13.
 */
@Controller
@RequestMapping("/homeController")
public class HomeControl extends BaseController {

    @Autowired
    HomeService homeService;

    @RequestMapping("/readMenu")
    @ResponseBody
    public JsonResult readMenu(Integer pageNum,Integer pageSize){
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 0;
        }
        JsonResult json = new JsonResult();
        Map<String,Object> map=new HashedMap();
        try {
            List<AppMenu> list = homeService.queryAllAppMenuByPage(pageNum,pageSize);
            map.put("list",list);
            Long total = homeService.queryAllAppMenuCount();
            map.put("total",total);
            if (list.size() > 0){
                json.setSuccess(true);

                json.setMessage("获取成功");

            } else {
                json.setSuccess(true);

                json.setMessage("菜单为空");

            }
        } catch (Exception e){
            e.printStackTrace();
            json.setMessage("获取失败");
            json.setSuccess(false);
        }
        json.setData(map);
        return json;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public JsonResult editMenu(Long id, String title,String status){
        JsonResult json = new JsonResult();
        try {
                homeService.editMenu(id,title,status);
                json.setSuccess(true);
                json.setMessage("修改成功");
        } catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("修改失败");
        }
        return json;
    }

    @RequestMapping("/queryDetails")
    @ResponseBody
    public JsonResult queryHuZhuDetails(Integer pageNum,Integer pageSize){
        JsonResult json = new JsonResult();
        Map<String,Object> map = new HashMap<>();
        try {
            List<HelpingInfo> list1 = getList(homeService.queryHeleInfoByTypeAndPage("互助计划",pageNum,pageSize));
            map.put("list1",list1);
            Long total1 = homeService.queryHeleCount("互助计划");
            map.put("total1",total1);

            List<HelpingInfo> list2 =getList(homeService.queryHeleInfoByTypeAndPage("承诺书",pageNum,pageSize)) ;
            map.put("list2",list2);
            Long total2 = homeService.queryHeleCount("承诺书");
            map.put("total2",total2);

            List<HelpingInfo> list3 = getList(homeService.queryHeleInfoByTypeAndPage("互助保障",pageNum,pageSize));
            map.put("list3",list3);
            Long total3 = homeService.queryHeleCount("互助保障");
            map.put("total3",total3);

            List<HelpingInfo> list4 =getList(homeService.queryHeleInfoByTypeAndPage("常见问题",pageNum,pageSize));
            map.put("list4",list4);
            Long total4 = homeService.queryHeleCount("常见问题");
            map.put("total4",total4);

            List<HelpingInfo> list5 = getList(homeService.queryHeleInfoByTypeAndPage("如何加入",pageNum,pageSize));
            map.put("list5",list5);
            Long total5 = homeService.queryHeleCount("如何加入");
            map.put("total5",total5);

            List<HelpingInfo> list6 = getList(homeService.queryHeleInfoByTypeAndPage("认领协议",pageNum,pageSize));
            map.put("list6",list6);
            Long total6 = homeService.queryHeleCount("认领协议");
            map.put("total6",total6);

            List<HelpingInfo> list7 =getList(homeService.queryHeleInfoByTypeAndPage("用户信誉依据",pageNum,pageSize)) ;
            map.put("list7",list7);
            Long total7 = homeService.queryHeleCount("用户信誉依据");
            map.put("total7",total7);

            List<HelpingInfo> list8 = getList(homeService.queryHeleInfoByTypeAndPage("加盟入驻",pageNum,pageSize));
            map.put("list8",list8);
            Long total8 = homeService.queryHeleCount("加盟入驻");
            map.put("total8",total8);

            List<HelpingInfo> list9 = getList(homeService.queryHeleInfoByTypeAndPage("注册声明",pageNum,pageSize));
            map.put("list9",list9);
            Long total9 = homeService.queryHeleCount("注册声明");
            map.put("total9",total9);

            List<HelpingInfo> list10 = getList(homeService.queryHeleInfoByTypeAndPage("分享下载页",pageNum,pageSize));
            map.put("list10",list10);
            Long total10 = homeService.queryHeleCount("分享下载页");
            map.put("total10",total10);

            List<HelpingInfo> list11 = homeService.queryImage(pageNum,pageSize);
            map.put("list11",list11);
            Long total11 = homeService.queryImageCount();
            map.put("total10",total11);

            json.setData(map);
            json.setSuccess(true);
            json.setMessage("获取成功");
        } catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取失败");
        }
        return json;
    }

    @RequestMapping("/deleteDetails")
    @ResponseBody
    public JsonResult deleteHuZhuDetails(Long id){
        JsonResult json = new JsonResult();
        try {
            String s1 = homeService.queryDetailsById(id);
            if (s1 != null || s1 != ""){
                homeService.deleteDetails(id);
                json.setSuccess(true);
                json.setMessage("删除成功");
            } else {
                json.setSuccess(false);
                json.setMessage("所选标签不存在，无法删除");
            }
        } catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("删除失败");
        }
        return json;
    }

    @RequestMapping("/uploadDetails")
    @ResponseBody
    public JsonResult uploadHuZhuDetails(String type,String s1,String s2,String s3,String s4,String s5,String s6,String s7,String s8){
        JsonResult json = new JsonResult();
        try {
            homeService.uploadDetails(type,s1,s2,s3,s4,s5,s6,s7,s8);
            json.setSuccess(true);
            json.setMessage("保存成功");
        } catch (Exception e){
            json.setSuccess(false);
            json.setMessage("保存失败");
        }
        return json;
    }

    @RequestMapping("/editDetails")
    @ResponseBody
    public JsonResult editHuZhuDetails(String id,String s1,String s2,String s3,String s4,String s5,String s6,String s7,String s8){
        JsonResult json = new JsonResult();
        try {
            homeService.editHuZhuDetails(id,s1,s2,s3,s4,s5,s6,s7,s8);
            json.setSuccess(true);
            json.setMessage("修改成功");
        } catch (Exception e){
            System.out.print(e);
            json.setSuccess(false);
            json.setMessage("修改失败");
        }
        return json;
    }

    public static String subContent(String content){
        String  str="";

        if(content.length()>50){
            str=content.substring(0,50)+"...";
        }else{
            str=content;
        }

        return str;


    }

    public  static  List<HelpingInfo> getList(List<HelpingInfo> list){
        List<HelpingInfo> newList=new ArrayList<>();

        for(int i=0;i<list.size();i++){
            HelpingInfo helpingInfo=list.get(i);
            String newContent=subContent(helpingInfo.getContext());
            helpingInfo.setNewContext(newContent);
            newList.add(helpingInfo);
        }
        return newList;
    }
}
